package com.kgd.news.controller.admin;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.upload.UploadFile;
import com.kgd.news.entity.Image;
import com.kgd.news.entity.Media;
import com.kgd.news.utils.Api;
import com.kgd.news.utils.BaseController;
import com.kgd.news.utils.DateUtils;
import com.kgd.news.utils.StringUtils;
import com.kgd.news.utils.wx.WxHelper;

import java.sql.SQLException;
import java.util.List;

/**
 * @Email: huangsanm@foxmail.com
 * @Date: 2017/10/9
 * @Time: 下午3:03
 */
public class MediaController extends BaseController {

    public void uplodImg() {
        UploadFile uploadFile = getFile();
        String filePath = "";
        if (uploadFile != null && !StringUtils.isEmpty(uploadFile.getFileName())) {
            filePath = getFileUploadPath(uploadFile);
        }
        Media m = new Media();
        m.setImg(filePath);
        m.setType(1);
        if (m.save()) {
            renderJson(new Api(Api.S_200, m).get());
        } else {
            renderJson(new Api(Api.E_201, "'").get());
        }
    }

    @Override
    public void list() {
//        if (imageid != null) {
//            getSession().setAttribute("imageid", imageid);
//        } else {
//            imageid = (String) getSession().getAttribute("imageid");
//        }
        String categoryid = getPara("categoryid");
        String imageid = getPara("imageid");
        String sql = "from media where type = 1 ";
        String type = "image";
        if (!StringUtils.isEmpty(imageid)) {
            sql += " and otherid = " + imageid;
            type += imageid;
        }
        sql += " order by id desc";
        Page<Media> list = Media.dao.paginate(getPageIndex(type), 100, "select *", sql);
        setAttr("data", list);
        setAttr("categoryid", categoryid);
        setAttr("imageid", imageid);
        render("media_list.html");
//        WxHelper.requestUrl("https://qqh666.cn/zxbxspj/api/getQrCodeByB");
    }

    @Override
    public void save() {
        //        这一段代码做了什么呢
//        request = new MultipartRequest(request);
//        UploadFile uploadFile = getFile("img");
//        String filePath = "";
//        if (uploadFile != null && !StringUtils.isEmpty(uploadFile.getFileName())) {
//            filePath = getFileUploadPath(uploadFile);
//        }
        getFile();
        Media media = getModel(Media.class, "media");
        if (media != null && media.getId() != null) {
            media.update();
        }
        CacheKit.removeAll("imageAndVideo");
        CacheKit.removeAll("image");
        list();
    }

    @Override
    public void edit() {
        Integer id = getParaToInt("id");
        Media media = Media.dao.findById(id);
        setAttr("media", media);
        render("media_edit.html");
    }

    @Override
    public void del() {
        String ids = getPara("ids");
        Db.update("delete from media where id in (" + ids + ")");
        CacheKit.removeAll("image");
        CacheKit.removeAll("imageAndVideo");
        setAttr("categoryid", getPara("categoryid"));
        setAttr("imageid", getPara("imageid"));
        list();
    }
}
